package com.cysnake.quitsmoke.activity;

import java.util.Date;

import com.cysnake.quitsmoke.R;
import com.cysnake.quitsmoke.VO.CostVO;
import com.cysnake.quitsmoke.common.dao.CostDao;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class CostActivity extends Activity {
	/** Called when the activity is first created. */
	Button submitButton;
	Button cancelButton;
	EditText cigCostEdit;
	EditText cigNameEdit;
	CostDao costDao;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.cost);
		modify();
	}

	private void modifyButton() {
		submitButton = (Button) findViewById(R.id.costSubmitButton);
		OnClickListener submitButtonLsn = new OnClickListener() {

			@Override
			public void onClick(View v) {
				CostVO cost = new CostVO();
				Editable text = cigNameEdit.getText();
				if (text.length() > 0) {
					cost.setName(text.toString());
				}
				text = cigCostEdit.getText();
				if (text.length() > 0) {
					cost.setCostnum(Double.parseDouble(text.toString()));

				}
				cost.setBuytime(new Date());
				Log.i("test", "CostActivity:    " + cost);
				costDao = costDao.open();
				if (costDao.create(cost) >= 0) {
					costDao.close();
					new AlertDialog.Builder(CostActivity.this)
							.setTitle(getString(R.string.cost_dailog_title))
							.setMessage(
									getString(R.string.cost_dailog_message_success))
							.setPositiveButton(
									getString(R.string.cost_dailog_commit),
									null).show();
				} else {
					costDao.close();
					new AlertDialog.Builder(CostActivity.this)
							.setTitle(getString(R.string.cost_dailog_title))
							.setMessage(
									getString(R.string.cost_dailog_message_fail))
							.setPositiveButton(
									getString(R.string.cost_dailog_commit),
									null).show();
				}

			}
		};
		submitButton.setOnClickListener(submitButtonLsn);

	}

	private void modifyEditText() {
		cigNameEdit = (EditText) findViewById(R.id.cigName);
		cigCostEdit = (EditText) findViewById(R.id.cigCost);
	}

	private void modify() {
		modifyEditText();
		modifyButton();
		costDao = new CostDao(this);
	}
}